<?php
Prado::using('Application.common.common');
class EditFoodPlace extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = "Admin | Edit Food Place";
			$this->AdminMenu->lnkManageFoodPlace->CssClass = "active";
			
			if ($this->Request->contains('m')) {
				$this->Notice->Type = UserNoticeType::NoticeAdmin;
				if ($this->Request['m'] == 'cfp')
					$this->Notice->Text = $this->Application->getModule('message')->translate('ADD_SUCCESS','Food Place','');
				else if ($this->Request['m'] == 'ufp')	
					$this->Notice->Text = $this->Application->getModule('message')->translate('UPDATE_SUCCESS','Food Place','');
				
			}
			
			// Populate MRT List			
			$list = array_reverse(MRTRecord::finder()->getAllItems());
			$list[] = array("Name" => "Not Sure", "ID" => -1);
			$list = array_reverse($list);
			$this->cboMRTSelector->DataSource = $list;//MRTRecord::finder()->getAllItems();
			$this->cboMRTSelector->DataBind();
			
			// Populate Price Range List
			$this->cboPriceRangeSelector->DataSource = PriceRangeRecord::finder()->getAllItems();
			$this->cboPriceRangeSelector->DataBind();
			
            // Populate Close Time List
			$this->cboCloseTimeSelector->DataSource = CloseTimeRecord::finder()->getAllItems();
            $this->cboCloseTimeSelector->DataBind();
			
			// Populate Cuisine List
			$this->cboCuisine1Selector->DataSource = $this->cboCuisine2Selector->DataSource = $this->cboCuisine3Selector->DataSource =  CuisineRecord::finder()->getAllItems();
			$this->cboCuisine1Selector->DataBind();
			$this->cboCuisine2Selector->DataBind();
			$this->cboCuisine3Selector->DataBind();
			
			$this->txtFoodPlaceID->Value = ($this->Request->contains('fid')) ? TPropertyValue::ensureInteger($this->Request['fid']) : 0;	
			$foodPlaceRecord = FoodPlaceRecord::finder()->findByPk($this->txtFoodPlaceID->Value);
			if ($foodPlaceRecord instanceof FoodPlaceRecord)
			{
				$this->txtName->Text=$foodPlaceRecord->Name;
				$this->txtHighlights->Text=$foodPlaceRecord->Highlights;
				$this->txtOperatingHours->Text=$foodPlaceRecord->OperatingHours;
				$this->txtLocation->Text=$foodPlaceRecord->Location;
				$this->txtContact->Text=$foodPlaceRecord->Contact;
				$this->txtMapGoogle->Text=$foodPlaceRecord->MapGoogle;
				$this->txtDetails->Text=$foodPlaceRecord->PlaceDetails;				
				$this->cboMRTSelector->SelectedValue = $foodPlaceRecord->NearestMRT;	
				$this->cboPriceRangeSelector->SelectedValue = $foodPlaceRecord->PriceRange;	
				$this->cboCloseTimeSelector->SelectedValue = $foodPlaceRecord->OpenUntil;	
				
				if ($foodPlaceRecord->IsRecommended == 1)
					$this->chkRecommendedFoodPlace->Checked = true;
				if ($foodPlaceRecord->IsNew == 1)
					$this->chkNewFoodPlace->Checked = true;				

				$currentYear = date('Y', $foodPlaceRecord->CreateDate);
				$currentMonth = date('m', $foodPlaceRecord->CreateDate);
				$this->imgMainPhoto->ImageUrl = $this->Request->UrlManagerModule->UrlPrefix."/useruploads/photos/food-place/".$currentYear."/".$currentMonth."/".$foodPlaceRecord->MainPhoto;
				
				$criteria = new TActiveRecordCriteria;
				$criteria->Condition = "food_place_id = " . $foodPlaceRecord->ID;
				$items = FoodPlaceCuisineRecord::finder()->findAll($criteria);				
				
				foreach ($items as $foodPlaceCuisineRecord) 
				{
					if ($foodPlaceCuisineRecord->SortOrder == 1)
						$this->cboCuisine1Selector->SelectedValue = $foodPlaceCuisineRecord->CuisineID;							
					if ($foodPlaceCuisineRecord->SortOrder == 2)
						$this->cboCuisine2Selector->SelectedValue = $foodPlaceCuisineRecord->CuisineID;							
					if ($foodPlaceCuisineRecord->SortOrder == 3)
						$this->cboCuisine3Selector->SelectedValue = $foodPlaceCuisineRecord->CuisineID;																																
				}
			}
		}
	}
	
	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{						
			try {
				$user = UserRecord::finder()->findByPk($this->Application->User->ID);
				if ($user instanceof UserRecord)
				{						
					$foodPlaceRecord = FoodPlaceRecord::finder()->findByPk($this->txtFoodPlaceID->Value);
					$foodPlaceRecord->Name = $this->txtName->SafeText;
					$foodPlaceRecord->Highlights = $this->txtHighlights->SafeText;					
					$foodPlaceRecord->NearestMRT = $this->cboMRTSelector->SelectedValue;
					$foodPlaceRecord->PriceRange = $this->cboPriceRangeSelector->SelectedValue;
					$foodPlaceRecord->OpenUntil = $this->cboCloseTimeSelector->SelectedValue;										
					$foodPlaceRecord->Location = $this->txtLocation->SafeText;					
					$foodPlaceRecord->OperatingHours = $this->txtOperatingHours->SafeText;					
					$foodPlaceRecord->Contact = $this->txtContact->SafeText;
					$foodPlaceRecord->PlaceDetails = $this->txtDetails->SafeText;
					$foodPlaceRecord->MapGoogle = $this->txtMapGoogle->Text;
					$foodPlaceRecord->IsRecommended = "0";
					if ($this->chkRecommendedFoodPlace->Checked)	
						$foodPlaceRecord->IsRecommended = "1";
					$foodPlaceRecord->IsNew = "0";
					if ($this->chkNewFoodPlace->Checked)	
						$foodPlaceRecord->IsNew = "1";	
					$foodPlaceRecord->ModifyBy = $user->ID;
					$foodPlaceRecord->ModifyDate = time();
					
					$hashImage = "";
					$currentYear = date("Y");
					$currentMonth = date("m");
					$filePath = dirname($this->Request->ApplicationFilePath).DIRECTORY_SEPARATOR."useruploads".DIRECTORY_SEPARATOR."photos".DIRECTORY_SEPARATOR."food-place".DIRECTORY_SEPARATOR.$currentYear.DIRECTORY_SEPARATOR;
					Common::check_dir($filePath);
					$filePath = $filePath.$currentMonth.DIRECTORY_SEPARATOR;				
					Common::check_dir($filePath);
					
					if($this->fuMainPhoto->HasFile) 
					{			
						$hashStr=md5(uniqid(time()));
						$imageExt=strtolower(array_pop(explode('.',$this->fuMainPhoto->FileName)));
						$hashImage=$hashStr.'.'.$imageExt;	
						$source = $filePath.$hashStr.$this->Application->Parameters['AFFFIX_ORG'].'.'.$imageExt;
						$this->fuMainPhoto->saveAs($source, true);	
						$foodPlaceRecord->MainPhoto=$hashImage;
						
						Common::saveFoodPlaceMainPhoto($source, $filePath, $hashImage, $hashStr, $imageExt);
						
						// create photo 0
						$photoRecord = new PhotoRecord;					
						$photoRecord->PhotoFile = $hashImage;
						$photoRecord->FoodPlaceID = $foodPlaceRecord->ID;
						$photoRecord->SortOrder = 0;
						$photoRecord->CreateBy = $user->ID;
						$photoRecord->CreateDate = time();			
						$photoRecord->save();
					}
										
					$foodPlaceRecord->save();
					
					// Save food place cuisine
					$criteria = new TActiveRecordCriteria;
					$criteria->Condition = "food_place_id = " . $foodPlaceRecord->ID;
					FoodPlaceCuisineRecord::finder()->deleteAll($criteria);					
					
					if($this->cboCuisine1Selector->SelectedValue > 0) 
					{
						Common::saveFoodPlaceCuisine(1,$foodPlaceRecord->ID,$this->cboCuisine1Selector->SelectedValue,$user->ID);						
					}
					
					if($this->cboCuisine2Selector->SelectedValue > 0) 
					{
						Common::saveFoodPlaceCuisine(2,$foodPlaceRecord->ID,$this->cboCuisine2Selector->SelectedValue,$user->ID);						
					}
					
					if($this->cboCuisine3Selector->SelectedValue > 0) 
					{
						Common::saveFoodPlaceCuisine(3,$foodPlaceRecord->ID,$this->cboCuisine3Selector->SelectedValue,$user->ID);						
					}
													
					$this->Response->redirect($this->Service->ConstructUrl('admin.EditFoodPlace', array('fid'=>$foodPlaceRecord->ID, 'm'=>'ufp')));			
				}
				else
				{					
					$this->Notice->Type = UserNoticeType::Error;
					$this->Notice->Text = $this->Application->getModule('message')->translate('ITEM_NOT_FOUND','user');
				}
			}
			catch(TException $e)
			{
				$this->Notice->Type = UserNoticeType::Error;
				$this->Notice->Text = $e;
			}
		}
	}
	
	protected function saveFoodPlacePhoto($photoFull, $photoThumb, $sortOrder, $userID, $tourDestinationID)
	{
		$filePath=dirname($this->Request->ApplicationFilePath).DIRECTORY_SEPARATOR."useruploads".DIRECTORY_SEPARATOR."photos".DIRECTORY_SEPARATOR."tour-destination".DIRECTORY_SEPARATOR;		
		$destinationPhotoRecord = new DestinationPhotoRecord;		
		
		$hashStr=md5(uniqid(time()));
		$imageExt=strtolower(array_pop(explode('.',$photoFull->FileName)));
		$hashImage=$hashStr.'.'.$imageExt;		
		$photoFull->saveAs($filePath.$hashImage, true);
		$destinationPhotoRecord->PhotoFull = $hashImage;
		
		$hashStr=md5(uniqid(time()));
		$imageExt=strtolower(array_pop(explode('.',$photoThumb->FileName)));
		$hashImage=$hashStr.'.'.$imageExt;		
		$photoThumb->saveAs($filePath.$hashImage, true);
		$destinationPhotoRecord->PhotoThumb = $hashImage;		

		$destinationPhotoRecord->DestinationId = $tourDestinationID;
		$destinationPhotoRecord->SortOrder = $sortOrder;							
		$destinationPhotoRecord->CreateBy = $userID;
		$destinationPhotoRecord->CreateDate = time();
		$destinationPhotoRecord->save();
	}
	
	protected function btnCancel_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("admin.ManageFoodPlaces"));				
	}
}

?>